﻿using NPOI.HSSF.UserModel;
using OMG.Fill.Mulberry.Model.Ent;
using OMG.Fill.Mulberry.Service;
using OMG.Fill.Mulberry.UI.ActionSafety;
using ProjectShare;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Http;
namespace OMG.Fill.Mulberry.Model.UI.Controllers
{
    public class ReportController : ApiController
    {
        ReportServices reportServices = new ReportServices();
        DictServices dictServices = new DictServices();

        //[ApiActionAuthorize("获取填报基本信息T_Report分页列表对象操作")]
        //[ApiActionException]
        public Pager<List<ReportModel>> PostPageList(ReportQuery query)
        {
            return reportServices.GetPageList(query);
        }
        public Pager<List<ReportModel>> PostReviewPageList(ReportQuery query)
        {
            if(!string.IsNullOrEmpty(query.FillInYear))
            {
                query.FillInYear = dictServices.GetPZKey(query.FillInYear,"ReportYear");
            }
            if (query.AreaCode == "" || query.AreaCode == "null") {
                query.AreaCode = AppUser.GetUser().AreaCode;
            }
            return reportServices.GetReviewPageList(query);
        }
        //[ApiActionAuthorize("新增填报基本信息T_Report指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Post(ReportEntity model) {
            return reportServices.AddInfo(model);
        }
        //[ApiActionAuthorize("修改填报基本信息T_Report指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Put(ReportEntity model)
        {
            return reportServices.UpdateInfo(model);
        }
        //[ApiActionAuthorize("删除填报基本信息T_Report指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Delete(string id)
        {
            return reportServices.DeleteInfo(id);
        }
        //[ApiActionAuthorize("获取填报基本信息T_Report指定对象操作")]
        //[ApiActionException]
        public ReportDataModel Get(string id, string reportCode)
        {
            return reportServices.GetReportData(id, reportCode);
        }
        public ResResult<int> PostUpStatus(ReviewModel model)
        {
            return reportServices.UpStatus(model.Id, model.reportType, model.strMsg);
        }

        public ReportDataModel Get(string id, string reportCode,string AreaCode,string FillInYear) {
            return reportServices.GetReportData(id,reportCode,AreaCode, FillInYear);
        }
        public ReportDataModel Get(string id, string AreaCode, string FillInYear)
        {
            return reportServices.GetReportData(id, AreaCode, FillInYear);
        }
        /// <summary>
        /// 获取任务完成情况
        /// </summary>
        /// <param name="AreaCode"></param>
        /// <returns></returns>
        public ResResult<List<ReportReview>> GetReportReview(string AreaCode,string FillInYear,string Account)
        {
            return reportServices.GetReportReview(AreaCode, FillInYear, Account);
        }

        /// <summary>
        ///  获取本年和上一年的数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ReportDataModel PostData(MulNowModel model)
        {
            return reportServices.GetModel(model);
        }

        /// <summary>
        /// 导入数据
        /// </summary>
        /// <param name="ReportCode"></param>
        /// <param name="AreaCode"></param>
        /// <returns></returns>
        public List<ResResult<int>> PostImportData(string FillInYear,string ReportCode, string AreaCode)
        {
            List<ResResult<int>> obj = new List<ResResult<int>>();
            ResResult<int> SingleObj = new ResResult<int>();
            HttpContext curContext = HttpContext.Current;
            HttpFileCollection files = curContext.Request.Files;
            if (curContext.Request.Files.Count != 0)
            {
                HttpPostedFile upfile = files[0];
                if (upfile != null && upfile.ContentLength > 0)
                {
                    string temppath = curContext.Server.MapPath("~/App_Data/temppath");
                    if (!Directory.Exists(temppath))//判断文件夹是否存在 
                    {
                        Directory.CreateDirectory(temppath);//不存在则创建文件夹 
                    }
                    string filetype = Path.GetExtension(upfile.FileName);
                    string filenewName = Guid.NewGuid().ToString().Replace("-", "") + filetype;
                    string strpath = Path.Combine(temppath, filenewName);
                    upfile.SaveAs(strpath);
                    string FillInYearKey = dictServices.GetKey(FillInYear);
                    switch (ReportCode) {
                        //桑园面积统计表
                        case "MulAreaStatist":
                            MulAreaStatistServices ms = new MulAreaStatistServices();
                            var BasicList = ms.ExcelToModel(strpath, filetype, "", 2);
                            foreach (var item in BasicList) {
                                item.year = FillInYearKey;
                                if (reportServices.CheckRepCreate(ReportCode, AreaCode, FillInYearKey))
                                {
                                    SingleObj.status = -1;
                                    SingleObj.message = item.year + "的报表没有填报数据！！";
                                    obj.Add(SingleObj);
                                } else
                                {
                                    string ReportId = reportServices.GetReportId(ReportCode, AreaCode, FillInYearKey);
                                    item.ReportId = ReportId;
                                    //判断list中是否初始化过报表
                                    if (ms.CheckRepCount(ReportId)) {
                                        //没初始化过进来
                                        SingleObj = ms.AddInfo(item);
                                    }else
                                    {
                                        SingleObj = ms.UpdateInfo(item);
                                    }
                                    SingleObj.status = DefaultKey.SuccessResult; ;
                                    SingleObj.message = item.year + "数据导入成功！！";
                                    reportServices.UpStatus(ReportId, "2", "");
                                    obj.Add(SingleObj);
                                }
                            }
                            break;
                        //表3全年蚕桑生产情况统计表
                        case "MulAnnualProducStatist":
                            MulAnnualProducStatistServices mulAnnualProducStatistServices = new MulAnnualProducStatistServices();
                            var ThreeList = mulAnnualProducStatistServices.ExcelToModel(strpath, filetype, "", 3);
                            foreach (var item in ThreeList)
                            {
                                item.FillInYear = FillInYearKey;
                                if (reportServices.CheckRepCreate(ReportCode, AreaCode, FillInYearKey))
                                {
                                    SingleObj.status = -1;
                                    SingleObj.message = item.year + "的报表没有填报数据！！";
                                    obj.Add(SingleObj);
                                }
                                else
                                {
                                    string ReportId = reportServices.GetReportId(ReportCode, AreaCode, FillInYearKey);
                                    item.ReportId = ReportId;
                                    //判断list中是否初始化过报表
                                    if (mulAnnualProducStatistServices.CheckRepCount(ReportId))
                                    {
                                        //没初始化过进来
                                        SingleObj = mulAnnualProducStatistServices.AddInfo(item);
                                    }
                                    else
                                    {
                                        SingleObj = mulAnnualProducStatistServices.UpdateInfo(item);
                                    }
                                    SingleObj.status = DefaultKey.SuccessResult; ;
                                    SingleObj.message = item.year + "数据导入成功！！";
                                    reportServices.UpStatus(ReportId, "2", "");
                                    obj.Add(SingleObj);
                                }
                            }
                            break;
                        //表4全年饲养蚕品种构成调查表
                        case "MulAnnualRear":
                            MulAnnualRearServices mulAnnualRearServices = new MulAnnualRearServices();
                            var FourList = mulAnnualRearServices.ExcelToModel(strpath, filetype, "", 3);
                            //foreach (var item in FourList) {
                            if (FourList != null && FourList.Count > 0) { 

                                if (reportServices.CheckRepCreate(ReportCode, AreaCode, FillInYearKey))
                                {
                                    SingleObj.status = -1;
                                    SingleObj.message = FillInYear + "的报表没有填报数据！！";
                                    obj.Add(SingleObj);
                                }else
                                {
                                    string ReportId = reportServices.GetReportId(ReportCode, AreaCode, FillInYearKey);
                                    //判断list中是否初始化过报表
                                    if (mulAnnualRearServices.CheckRepCount(ReportId))
                                    {
                                        //没初始化过进来
                                        SingleObj = mulAnnualRearServices.AddInfo(FourList,ReportId);
                                    }
                                    else
                                    {
                                        mulAnnualRearServices.DeleteData(ReportId);
                                        SingleObj = mulAnnualRearServices.AddInfo(FourList, ReportId);
                                    }
                                    SingleObj.status = DefaultKey.SuccessResult; ;
                                    SingleObj.message = FillInYear + "数据导入成功！！";
                                    reportServices.UpStatus(ReportId, "2", "");
                                    obj.Add(SingleObj);
                                }
                            }
                            break;
                        //表5蚕种生产情况统计表
                        case "MulEggsProducStatist":
                            MulEggsProducStatistServices mulEggsProducStatistServices = new MulEggsProducStatistServices();
                            var FiveList = mulEggsProducStatistServices.ExcelToModel(strpath, filetype, "", 3);
                            if (FiveList != null && FiveList.Count > 0) { 
                                if (reportServices.CheckRepCreate(ReportCode, AreaCode, FillInYearKey))
                                {
                                    SingleObj.status = -1;
                                    SingleObj.message = FillInYear + "的报表没有填报数据！！";
                                    obj.Add(SingleObj);
                                }
                                else
                                {
                                    string ReportId = reportServices.GetReportId(ReportCode, AreaCode, FillInYearKey);
                                    //判断list中是否初始化过报表
                                    if (mulEggsProducStatistServices.CheckRepCount(ReportId))
                                    {
                                        //没初始化过进来
                                        SingleObj = mulEggsProducStatistServices.AddInfo(FiveList,ReportId);
                                    }
                                    else
                                    {
                                        mulEggsProducStatistServices.DeleteData(ReportId);
                                        SingleObj = mulEggsProducStatistServices.AddInfo(FiveList, ReportId);
                                    }
                                    SingleObj.status = DefaultKey.SuccessResult; ;
                                    SingleObj.message = FillInYear + "数据导入成功！！";
                                    reportServices.UpStatus(ReportId, "2", "");
                                    obj.Add(SingleObj);
                                }
                            }
                            break;
                        //表6蚕桑综合利用情况调查表
                        case "MulComplexQuest":
                            MulComplexQuestServices mulComplexQuestServices = new MulComplexQuestServices();
                            var SixList = mulComplexQuestServices.ExcelToModel(strpath, filetype, "", 3);
                            foreach (var item in SixList)
                            {
                                if (reportServices.CheckRepCreate(ReportCode, AreaCode, FillInYearKey))
                                {
                                    SingleObj.status = -1;
                                    SingleObj.message = item.year + "的报表没有填报数据！！";
                                    obj.Add(SingleObj);
                                }
                                else
                                {
                                    string ReportId = reportServices.GetReportId(ReportCode, AreaCode, FillInYearKey);
                                    item.ReportId = ReportId;
                                    //判断list中是否初始化过报表
                                    if (mulComplexQuestServices.CheckRepCount(ReportId))
                                    {
                                        //没初始化过进来
                                        SingleObj = mulComplexQuestServices.AddInfo(item);
                                    }
                                    else
                                    {
                                        mulComplexQuestServices.DeleteData(item.ReportId);
                                        SingleObj = mulComplexQuestServices.AddInfo(item);
                                    }
                                    SingleObj.status = DefaultKey.SuccessResult; ;
                                    SingleObj.message = item.year + "数据导入成功！！";
                                    reportServices.UpStatus(ReportId, "2", "");
                                    obj.Add(SingleObj);
                                }
                            }
                            break;
                        //表8养蚕收益调查情况
                        case "MulSerEarnSurvey":
                            MulSerEarnSurveyServices mulSerEarnSurveyServices = new MulSerEarnSurveyServices();
                            var EightList = mulSerEarnSurveyServices.ExcelToModel(strpath, filetype, "", 3);
                            foreach (var item in EightList) {
                                string ReportId = reportServices.GetReportId(ReportCode, AreaCode, FillInYearKey);
                                item.ReportId = ReportId;
                                mulSerEarnSurveyServices.DeleteData(item.ReportId);
                            }
                            foreach (var item in EightList)
                            {
                                if (reportServices.CheckRepCreate(ReportCode, AreaCode, FillInYearKey))
                                {
                                    SingleObj.status = -1;
                                    SingleObj.message = FillInYear + "的报表没有填报数据！！";
                                    obj.Add(SingleObj);
                                }
                                else
                                {
                                    string ReportId = reportServices.GetReportId(ReportCode, AreaCode, FillInYearKey);
                                    item.ReportId = ReportId;
                                    //判断list中是否初始化过报表
                                    if (mulSerEarnSurveyServices.CheckRepCount(ReportId))
                                    {
                                        //没初始化过进来
                                        SingleObj = mulSerEarnSurveyServices.AddInfo(item);
                                    }
                                    else
                                    {
                                        SingleObj = mulSerEarnSurveyServices.AddInfo(item);
                                    }
                                    SingleObj.status = DefaultKey.SuccessResult; 
                                    SingleObj.message = FillInYear + "数据导入成功！！";
                                    reportServices.UpStatus(ReportId, "2", "");
                                    obj.Add(SingleObj);
                                }
                            }
                            break;
                        //表9套种作物收益调查情况
                        case "MulCropIncome":
                            MulCropIncomeServices mulCropIncomeServices = new MulCropIncomeServices();
                            var NineList = mulCropIncomeServices.ExcelToModel(strpath, filetype, "", 3);
                            foreach (var item in NineList)
                            {
                                string ReportId = reportServices.GetReportId(ReportCode, AreaCode, FillInYearKey);
                                item.ReportId = ReportId;
                                mulCropIncomeServices.DeleteData(item.ReportId);
                            }
                            foreach (var item in NineList)
                            {
                                if (reportServices.CheckRepCreate(ReportCode, AreaCode, FillInYearKey))
                                {
                                    SingleObj.status = -1;
                                    SingleObj.message = item.year + "的报表没有填报数据！！";
                                    obj.Add(SingleObj);
                                }
                                else
                                {
                                    string ReportId = reportServices.GetReportId(ReportCode, AreaCode, FillInYearKey);
                                    item.ReportId = ReportId;
                                    //判断list中是否初始化过报表
                                    if (mulCropIncomeServices.CheckRepCount(ReportId))
                                    {
                                        //没初始化过进来
                                        SingleObj = mulCropIncomeServices.AddInfo(item);
                                    }
                                    else
                                    {
                                        SingleObj = mulCropIncomeServices.AddInfo(item);
                                    }
                                    SingleObj.status = DefaultKey.SuccessResult; ;
                                    SingleObj.message = item.year + "数据导入成功！！";
                                    reportServices.UpStatus(ReportId, "2", "");
                                    obj.Add(SingleObj);
                                }
                            }
                            break;
                        //表10县生产信息调查统计表
                        case "MulProdInfoChart":
                            MulProdInfoChartServices mulProdInfoChartServices = new MulProdInfoChartServices();
                            var TenList = mulProdInfoChartServices.ExcelToModel(strpath, filetype, "", 2);
                            foreach (var item in TenList)
                            {
                                if (reportServices.CheckRepCreate(ReportCode, AreaCode, FillInYearKey))
                                {
                                    SingleObj.status = -1;
                                    SingleObj.message = FillInYear + "的报表没有填报数据！！";
                                    obj.Add(SingleObj);
                                }
                                else
                                {
                                    string ReportId = reportServices.GetReportId(ReportCode, AreaCode, FillInYearKey);
                                    item.ReportId = ReportId;
                                    //判断list中是否初始化过报表
                                    if (mulProdInfoChartServices.CheckRepCount(ReportId))
                                    {
                                        //没初始化过进来
                                        SingleObj = mulProdInfoChartServices.AddInfo(item);
                                    }
                                    else
                                    {
                                        mulProdInfoChartServices.DeleteData(item.ReportId);
                                        SingleObj = mulProdInfoChartServices.AddInfo(item);
                                    }
                                    SingleObj.status = DefaultKey.SuccessResult; ;
                                    SingleObj.message = FillInYear + "数据导入成功！！";
                                    reportServices.UpStatus(ReportId, "2", "");
                                    obj.Add(SingleObj);
                                }
                            }
                            break;
                        //表11蚕丝绸企业信息调查
                        case "MulSilkCompany":
                            MulSilkCompanyServices mulSilkCompanyServices = new MulSilkCompanyServices();
                            var ElevenList = mulSilkCompanyServices.ExcelToModel(strpath, filetype, "", 2);
                            foreach (var item in ElevenList)
                            {
                                if (reportServices.CheckRepCreate(ReportCode, AreaCode, FillInYearKey))
                                {
                                    SingleObj.status = -1;
                                    SingleObj.message = item.year + "的报表没有填报数据！！";
                                    obj.Add(SingleObj);
                                }
                                else
                                {
                                    string ReportId = reportServices.GetReportId(ReportCode, AreaCode, FillInYearKey);
                                    item.ReportId = ReportId;
                                    //判断list中是否初始化过报表
                                    if (mulSilkCompanyServices.CheckRepCount(ReportId))
                                    {
                                        //没初始化过进来
                                        SingleObj = mulSilkCompanyServices.AddInfo(item);
                                    }
                                    else
                                    {
                                        mulSilkCompanyServices.DeleteData(item.ReportId);
                                        SingleObj = mulSilkCompanyServices.AddInfo(item);
                                    }
                                    SingleObj.status = DefaultKey.SuccessResult; ;
                                    SingleObj.message = item.year + "数据导入成功！！";
                                    reportServices.UpStatus(ReportId, "2", "");
                                    obj.Add(SingleObj);
                                }
                            }
                            break;
                        //表12蚕丝绸企业信息调查
                        case "MulSilkwormField":
                            MulSilkwormFieldServices mulSilkwormFieldServices = new MulSilkwormFieldServices();
                            var twelveList = mulSilkwormFieldServices.ExcelToModel(strpath, filetype, "", 3);
                            foreach (var item in twelveList)
                            {
                                if (reportServices.CheckRepCreate(ReportCode, AreaCode, FillInYearKey))
                                {
                                    SingleObj.status = -1;
                                    SingleObj.message = FillInYear + "的报表没有填报数据！！";
                                    obj.Add(SingleObj);
                                }
                                else
                                {
                                    string ReportId = reportServices.GetReportId(ReportCode, AreaCode, FillInYearKey);
                                    item.ReportId = ReportId;
                                    //判断list中是否初始化过报表
                                    if (mulSilkwormFieldServices.CheckRepCount(ReportId))
                                    {
                                        //没初始化过进来
                                        SingleObj = mulSilkwormFieldServices.AddInfo(item);
                                    }
                                    else
                                    {
                                        mulSilkwormFieldServices.DeleteData(item.ReportId);
                                        SingleObj = mulSilkwormFieldServices.AddInfo(item);
                                    }
                                    SingleObj.status = DefaultKey.SuccessResult; ;
                                    SingleObj.message = FillInYear + "数据导入成功！！";
                                    reportServices.UpStatus(ReportId, "2", "");
                                    obj.Add(SingleObj);
                                }
                            }
                            break;
                        //表13企业信息调查
                        case "CompanyInfo":
                            CompanyInfoServices companyInfoServices = new CompanyInfoServices();
                            var thirteenList = companyInfoServices.ExcelToModel(strpath, filetype, "", 2);
                            foreach (var item in thirteenList)
                            {
                                if (reportServices.CheckRepCreate(ReportCode, AreaCode, FillInYearKey))
                                {
                                    SingleObj.status = -1;
                                    SingleObj.message = FillInYear + "的报表没有填报数据！！";
                                    obj.Add(SingleObj);
                                }
                                else
                                {
                                    string ReportId = reportServices.GetReportId(ReportCode, AreaCode, FillInYearKey);
                                    item.ReportId = ReportId;
                                    //没初始化过进来
                                    SingleObj = companyInfoServices.AddInfo(item);
                                    SingleObj.status = DefaultKey.SuccessResult; ;
                                    SingleObj.message = FillInYear + "数据导入成功！！";
                                    reportServices.UpStatus(ReportId, "2", "");
                                    obj.Add(SingleObj);
                                }
                            }
                            break;
                    }
                    Directory.Delete(temppath, true);
                }
            }
            return obj;
        }


        /// <summary>
        /// 动态表、季度表导入数据
        /// </summary>
        /// <param name="ReportId"></param>
        /// <returns></returns>
        public List<ResResult<int>> PostImportDataTwo(string ReportId,string ReportCode)
        {
            List<ResResult<int>> obj = new List<ResResult<int>>();
            ResResult<int> SingleObj = new ResResult<int>();
            HttpContext curContext = HttpContext.Current;
            HttpFileCollection files = curContext.Request.Files;
            if (curContext.Request.Files.Count != 0)
            {
                HttpPostedFile upfile = files[0];
                if (upfile != null && upfile.ContentLength > 0)
                {
                    string temppath = curContext.Server.MapPath("~/App_Data/temppath");
                    if (!Directory.Exists(temppath))//判断文件夹是否存在 
                    {
                        Directory.CreateDirectory(temppath);//不存在则创建文件夹 
                    }
                    string filetype = Path.GetExtension(upfile.FileName);
                    string filenewName = Guid.NewGuid().ToString().Replace("-", "") + filetype;
                    string strpath = Path.Combine(temppath, filenewName);
                    upfile.SaveAs(strpath);

                    switch (ReportCode)
                    {
                        //表2季度生产情况表
                        case "MulQuartProduc":
                            MulQuartProducServices mulQuartProducServices = new MulQuartProducServices();
                            var QuartList = mulQuartProducServices.ExcelToModel(strpath, filetype, "", 2);
                            MulQuartProducEntity model = new MulQuartProducEntity();
                            if (mulQuartProducServices.CheckRepCount(ReportId))
                            {
                                model.ReportId = ReportId;
                                mulQuartProducServices.AddInfo(model);
                            }
                            else
                            {
                                mulQuartProducServices.DeleteData(ReportId);
                                foreach (var item in QuartList)
                                {
                                    item.ReportId = ReportId;
                                    reportServices.UpStatus(ReportId, "2", "");
                                    SingleObj = mulQuartProducServices.AddInfo(item);
                                    SingleObj.status = DefaultKey.SuccessResult;
                                    SingleObj.message = "数据导入成功！！";
                                    obj.Add(SingleObj);
                                }
                            }
                            break;
                        //表7桑园套种农作物情况调查表
                        case "MulInterCrops":
                            MulInterCropsServices mulInterCropsServices = new MulInterCropsServices();
                            EditMulInterCropsModel editMulInterCropsModel = new EditMulInterCropsModel();
                            editMulInterCropsModel.ReportId = ReportId;
                            var InterList = mulInterCropsServices.ExcelToModel(strpath, filetype, "", 2);
                            //检查报表是否初始化过
                            if (mulInterCropsServices.CheckRepCount(ReportId))
                            {
                                //没初始化进来
                                List<MulInterCropsModel> templist = mulInterCropsServices.GetTabContent(ReportId, 0);
                                mulInterCropsServices.AddInfo(templist);
                            }
                            mulInterCropsServices.DeleteData(ReportId);
                            editMulInterCropsModel.ResultList = InterList;
                            foreach(var item in InterList)
                            {
                                SetListServices setListServices = new SetListServices();
                                item.ReportId = ReportId;
                                item.SortCode = setListServices.GetCode("MulInterCrops", item.SortName).ItemCode;
                            }
                            reportServices.UpStatus(ReportId, "2", "");
                            SingleObj = mulInterCropsServices.AddOrUpdateList(editMulInterCropsModel);
                            SingleObj.status = DefaultKey.SuccessResult;
                            SingleObj.message = "数据导入成功！！";
                            obj.Add(SingleObj);

                            break;
                    }
                    
                    Directory.Delete(temppath, true);
                }
            }
            return obj;
        }

        /// <summary>
        /// 模板下载
        /// </summary>
        /// <param name="ReportCode"></param>
        /// <returns></returns>
        public MemoryStream GetExcelTemplate(string ReportCode)
        {
            var workbook = new HSSFWorkbook();
            string fileIndex = HttpRuntime.AppDomainAppPath.ToString();
            string templateName = "";
            string sheetName = "";
            switch (ReportCode)
            {
                case "MulAreaStatist":
                    templateName = fileIndex + @"Excel\ExcelModel.xls";//模板文件路径
                    sheetName = "Sheet1";
                    break;
                case "MulAnnualProducStatist":
                    templateName = fileIndex + @"Excel\ExcelModel.xls";//模板文件路径
                    sheetName = "Sheet3";
                    break;
                case "MulAnnualRear":
                    templateName = fileIndex + @"Excel\ExcelModel.xls";//模板文件路径
                    sheetName = "Sheet4";
                    break;
                case "MulEggsProducStatist":
                    templateName = fileIndex + @"Excel\ExcelModel.xls";//模板文件路径
                    sheetName = "Sheet5";
                    break;
                case "MulComplexQuest":
                    templateName = fileIndex + @"Excel\ExcelModel.xls";//模板文件路径
                    sheetName = "Sheet6";
                    break;
                case "MulSerEarnSurvey":
                    templateName = fileIndex + @"Excel\ExcelModel.xls";//模板文件路径
                    sheetName = "Sheet8";
                    break;
                case "MulCropIncome":
                    templateName = fileIndex + @"Excel\ExcelModel.xls";//模板文件路径
                    sheetName = "Sheet9";
                    break;
                case "MulProdInfoChart":
                    templateName = fileIndex + @"Excel\ExcelModel.xls";//模板文件路径
                    sheetName = "Sheet10";
                    break;
                case "MulQuartProduc":
                    templateName = fileIndex + @"Excel\ExcelModel.xls";//模板文件路径
                    sheetName = "Sheet2";
                    break;
                case "MulInterCrops":
                    templateName = fileIndex + @"Excel\ExcelModel.xls";//模板文件路径
                    sheetName = "Sheet7";
                    break;
                case "MulSilkCompany":
                    templateName = fileIndex + @"Excel\ExcelModel.xls";//模板文件路径
                    sheetName = "Sheet11";
                    break;
                case "MulSilkwormField":
                    templateName = fileIndex + @"Excel\ExcelModel.xls";//模板文件路径
                    sheetName = "Sheet12";
                    break;
                case "CompanyInfo":
                    templateName = fileIndex + @"Excel\ExcelModel.xls";//模板文件路径
                    sheetName = "Sheet13";
                    break;

            }
            FileStream file = new FileStream(templateName, FileMode.Open, FileAccess.Read);//读模板文件
            HSSFWorkbook templateBook = new HSSFWorkbook(file);//读取模板文件信息转换成Excel工作薄
            HSSFSheet sheet = templateBook.GetSheet(sheetName) as HSSFSheet;//读取模板所在sheet位置
            sheet.CopyTo(workbook, "Sheet1", true, true);
            using (MemoryStream ms = new MemoryStream())
            {
                workbook.Write(ms);
                ms.Flush();
                ms.Position = 0;
                file.Close();
                return ms;
            }
        }

        /// <summary>
        /// 模板下载响应
        /// </summary>
        /// <param name="ReportCode"></param>
        public void GetTemplateData(string ReportCode)
        {
            //打开完成excel文件并下载
            HttpContext curContext = HttpContext.Current;
            // 设置编码和附件格式
            curContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.UTF8;
            curContext.Response.Charset = "";
            string strFileName = "";
            switch (ReportCode)
            {
                case "MulAreaStatist":
                    strFileName = "桑园面积统计表.xls";//文件名称
                    break;
                case "MulAnnualProducStatist":
                    strFileName = "全年蚕桑生产情况统计表.xls";//文件名称
                    break;
                case "MulAnnualRear":
                    strFileName = "全年饲养蚕品种构成调查表.xls";//文件名称
                    break;
                case "MulEggsProducStatist":
                    strFileName = "全年蚕种生产情况统计表.xls";//文件名称
                    break;
                case "MulComplexQuest":
                    strFileName = "蚕桑综合利用情况调查表.xls";//文件名称
                    break;
                case "MulSerEarnSurvey":
                    strFileName = "养蚕收益情况调查表.xls";//文件名称
                    break;
                case "MulCropIncome":
                    strFileName = "套种作物收益情况调查表.xls";//文件名称
                    break;
                case "MulProdInfoChart":
                    strFileName = "生产信息调查统计表.xls";//文件名称
                    break;
                case "MulQuartProduc":
                    strFileName = "季度生产情况表.xls";//文件名称
                    break;
                case "MulInterCrops":
                    strFileName = "桑园套种农作物情况调查表.xls";//文件名称
                    break;
                case "MulSilkCompany":
                    strFileName = "茧丝绸企业信息调查表.xls";//文件名称
                    break;
                case "MulSilkwormField":
                    strFileName = "桑蚕种场生产经营情况调查表.xls";//模板文件路径
                    break;
                case "CompanyInfo":
                    strFileName = "企业调查表.xls";//模板文件路径
                    break;
            }
            curContext.Response.AppendHeader("Content-Disposition",
                "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));
            curContext.Response.BinaryWrite(GetExcelTemplate(ReportCode).GetBuffer());
            curContext.Response.End();
        }

    }
    public class ReviewModel
    {
        public string Id { get; set; }
        public string reportType { get; set; }
        public string strMsg { get; set; }
    }

} 